﻿// ***********************************************************************
// Assembly         : MVC4Portal
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="MyProfileController.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using EmitMapper;
using InnovaApp.Layers.CommonLayer;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.DomainLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Layers.SecurityLayer.Models.MetaModels;
using InnovaApp.Portals.MVC4Portal.Models.JSonModels;
using InnovaApp.Portals.MVC4Portal.Models.MetaModels.ContactManagement;
using InnovaApp.Portals.MVC4Portal.Models.ViewModels;
using InnovaApp.Portals.MVC4Portal.Models.ViewModels.LayoutViewModels;
using INNOVACALL.Framework.Common;
using INNOVACALL.Framework.Common.Extensions;
using INNOVACALL.Framework.Common.InnovacallExceptions;
using INNOVACALL.Framework.Common.MVCHelpers;
using INNOVACALL.Framework.Common.MVCHelpers.Localization;
using System.Drawing;
using System.Drawing.Imaging;
using InnovaApp.Portals.MVC4Portal.Filters;
using INNOVACALL.Framework.Common.Models;

namespace InnovaApp.Portals.MVC4Portal.Controllers.ViewControllers
{
    /// <summary>
    /// Class MyProfileController
    /// </summary>
    public partial class LookupsController
    {
        /// <summary>
        /// Gets the competency.
        /// </summary>
        /// <param name="ajaxModel">The ajax model.</param>
        /// <returns>JsonResult.</returns>
        /// <exception cref="INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException"></exception>
        private JsonResult GetCompetency(LookupDetailFeed_JSonModel ajaxModel)
        {
            string eMethod = eMethodBase + "GetCompetency";
            object eParameters = new { ajaxModel = ajaxModel };
            JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            try
            {
                Competency_MetaModel model;
                if (ajaxModel.ItemToFeedId == -1)
                {
                    model = new Competency_MetaModel() { Id = -1, Name = string.Empty, Description = string.Empty, rowversion = null };
                }
                else
                {
                    using (DataRepositories _dataContext = new DataRepositories())
                    {
                        Competency dbCompetency = _dataContext.Competency_Rep.GetById(ajaxModel.ItemToFeedId);
                        model = ObjectMapperManager.DefaultInstance.GetMapper<Competency, Competency_MetaModel>().Map(dbCompetency);
                        if (model == null)
                        {
                            throw new INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException(eMethod, eParameters, Resources.ErrMsg_NotFound);
                        }
                    }
                }
                string htmlContent = base.RenderPartialViewToString("_LookupCompetency", model);
                toReturn.Data = new INNOVACALLAjaxResponse(
                    success: true,
                    domElementToUpdate: ajaxModel.HtmlElementToUpdate,
                    htmlContent: htmlContent,
                    scriptToExecute: null,
                    messageTitle: null,
                    message: null,
                    additionalData: model
                );
            }
            catch (Exception ex)
            {
                INNOVACALL.Framework.Common.InnovacallExceptions.CommonException iex = new INNOVACALL.Framework.Common.InnovacallExceptions.CommonException(eMethod, eParameters, ex.Message);
                iex.LogIt();
                toReturn.Data = new INNOVACALLAjaxResponse(
                    success: false,
                    domElementToUpdate: null,
                    htmlContent: null,
                    scriptToExecute: "$.fn.jqDialogAlert(a.messageTitle ,a.message,'');",
                    messageTitle: Resources.View_All_DialogErrorTitle,
                    message: ex.LayoutException(),
                    additionalData: null
                );
            }
            return toReturn;
        }

        /// <summary>
        /// Deletes a competency.
        /// </summary>
        /// <param name="ajaxModel">The ajax model.</param>
        /// <returns>JsonResult.</returns>
        /// <exception cref="INNOVACALL.Framework.Common.InnovacallExceptions.DataIntegrityException"></exception>
        private JsonResult DeleteCompetency(LookupDetailFeed_JSonModel ajaxModel)
        {
            string eMethod = eMethodBase + "DeleteCompetency";
            object eParameters = new { ajaxModel = ajaxModel };
            JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            try
            {
                Competency_MetaModel model;
                using (DataRepositories _dataContext = new DataRepositories())
                {
                    Competency dbCompetency = _dataContext.Competency_Rep.GetById(ajaxModel.ItemToFeedId);
                    if (dbCompetency != null) //Just refresh the grid
                    {
                        _dataContext.Competency_Rep.Remove(dbCompetency);
                        _dataContext.Save();
                    }
                }
                toReturn.Data = new INNOVACALLAjaxResponse(
                    success: true,
                    domElementToUpdate: ajaxModel.HtmlElementToUpdate,
                    htmlContent: null,
                    scriptToExecute: @"$.fn.LookupUpdate(true,a.domElementToUpdate);",
                    messageTitle: null,
                    message: null,
                    additionalData: null
                );
            }
            catch (Exception ex)
            {
                INNOVACALL.Framework.Common.InnovacallExceptions.CommonException iex = new INNOVACALL.Framework.Common.InnovacallExceptions.CommonException(eMethod, eParameters, ex.Message);
                iex.LogIt();
                toReturn.Data = new INNOVACALLAjaxResponse(
                    success: false,
                    domElementToUpdate: null,
                    htmlContent: null,
                    scriptToExecute: "$.fn.jqDialogAlert(a.messageTitle ,a.message,'');",
                    messageTitle: Resources.View_All_DialogErrorTitle,
                    message: ex.LayoutException(),
                    additionalData: null
                );
            }
            return toReturn;
        }

        /// <summary>
        /// Lookups the competency add edit.
        /// </summary>
        /// <param name="language">The language.</param>
        /// <param name="model">The model.</param>
        /// <returns>JsonResult.</returns>
        [HttpPost, Authorize, CacheManager(enCacheMode.NoCache)]
        public JsonResult LookupCompetencyAddEdit(string language, Competency_MetaModel model)
        {
            string eMethod = eMethodBase + "ProfileDetailCompetencyAddEdit[Post]";
            string scriptToExecute = string.Empty;
            object eParameters = new { language = language, model = model };
            JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            INNOVACALL.Framework.Common.MVCHelpers.Localization.CommonLocalization.SetCurrentThreadLanguage(language);
            Competency dbCompetency;
            Guid userId;
            using (TransactionScope _ts = new TransactionScope())
            {
                try
                {
                    using (DataRepositories _dataContext = new DataRepositories())
                    {
                        if (model.Id == -1)
                        {
                            dbCompetency = new Competency()
                            {
                                Id = model.Id,
                                Name = model.Name,
                                Description = model.Description,
                                rowversion = null

                            };
                            _dataContext.Competency_Rep.Add(dbCompetency);
                        }
                        else
                        {
                            dbCompetency = _dataContext.Competency_Rep.GetById(model.Id);
                            dbCompetency.Name = model.Name;
                            dbCompetency.Description = model.Description;
                        }

                        _dataContext.Save();
                        _ts.Complete();
                        //ToDo : LJO To review
                        toReturn.Data = new INNOVACALLAjaxResponse(
                            success: true,
                            domElementToUpdate: string.Empty,
                            htmlContent: string.Empty,
                            scriptToExecute: @"$(""#LookupDetailDialogDiv"").dialog(""close"");$.fn.LookupBuildGrid();",
                            messageTitle: null,
                            message: null,
                            additionalData: null
                        );
                    }
                }
                catch (Exception ex)
                {
                    INNOVACALL.Framework.Common.InnovacallExceptions.CommonException iex = new INNOVACALL.Framework.Common.InnovacallExceptions.CommonException(eMethod, eParameters, ex.Message);
                    iex.LogIt();
                    toReturn.Data = new INNOVACALLAjaxResponse(
                        success: false,
                        domElementToUpdate: null,
                        htmlContent: null,
                        scriptToExecute: "$.fn.jqDialogAlert(a.messageTitle ,a.message,'');",
                        messageTitle: Resources.View_All_DialogErrorTitle,
                        message: ex.LayoutException(),
                        additionalData: null
                    );
                }
            }
            return toReturn;
        }

    }
}
